<?php

namespace App\Http\Controllers\Login;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Service\User\UserService;
use App\Http\Controllers\Controller;
//use Illuminate\Support\Facades\Redis;
use Session,Config;
use Cache;

class LoginController extends Controller
{
    public function login()
    {
        return view('Login.login');
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Laravel\Lumen\Http\Redirector|void
     * 登录
     */
    public function doLogin(Request $request)
    {

        $where = [];

        $data['username'] = $request->input('username');
        $data['password'] = $request->input('password');

        $user = new UserService();
        $ret =  $user->login($data);

        $result = '';
        if($ret['error_code'] == 0)
        {
            $result = $ret['data'];
            Session::put(Config::get('custom.setting.MEMBER_TOKEN'),$result['token']);
            Session::put(Config::get('custom.setting.MEMBER_INFO'),$result['username']);
            Session::put(Config::get('custom.setting.MEMBER_ROLE'),1);
            unset($ret['data']);

            return $ret;
        }else{
            echo "<script>confirm('账号或密码不对，请从新登陆？')?location.href='/  ' : history.back() ;</script>";
            exit();
        }

    }

    /**
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Laravel\Lumen\Http\Redirector|void
     *
     * 退出登录
     */
    public function logout()
    {
        $where = [];
        $where['token'] = Session::get('member_token');

        Session::forget('member_token');
        Session::forget('member_token');
        return redirect('/');

    }



}
